<?php
$script = 'survey';
require_once '../../controller.php';
require_once '../../header.php';
require_once '../../classes/Survey.php';
if(isset($_SESSION['logged_in'])) {

	$instructor_id = $_SESSION['logged_in'];

}


// Displaying Survey Results.
if(isset($_REQUEST['sid'])) {

	$survey_id = $_REQUEST['sid'];

	echo '<h1 align="center">Survey Results</h1>';
	$r = new Response();
	$s = new Survey();
	$survey = $s->getSurvey($survey_id);

	echo '<p align="center"> Survey Name: '.$survey['name'].'</p><br>';

	$questions = $s->getQuestions($survey_id);

	if($questions){
		foreach ($questions as $i => $value) {
			$question_id[] = $value->id;
			$type[] = $value->type;
			$question_value[] = $value->question;
			$response1[]= $value->response1;
			$response2[]= $value->response2;
			$response3[]= $value->response3;
			$response4[]= $value->response4;
			$response5[]= $value->response5;
			$response6[]= $value->response6;
		}
	}
}
$num = sizeof($question_id);
$datafile = "datafile.csv";
$handle = fopen($datafile,'w') or die("can't open file");
echo "<p align='right'><a href='result.php?sid=$survey_id'><font color='blue' size='8em'><u>Raw Output</u></font></a></p>"; 
print "<p align='right'><a href='export.php'><font color='blue' size='8em'><u>Export Results</u></font></a></p><br><br>";
print "<table  cellpadding='10px'>";
for($k=0;$k<$num;$k++){
	$question_num = $k+1;
	
	if($type[$k]=='Radio Button' || $type[$k]=='Drop Down'){
		$all_responses = $r->getAllResponseValues($question_id[$k]);
		$total_responses = sizeof($all_responses); 
		print "<tr><td width='400px'><b>($question_num.)-->$question_value[$k]</b></td></tr>";
		fwrite($handle,$question_value[$k].',');
		print "<tr><td>&nbsp;</td></tr>";
		print "<tr><td><b>Options</b></td><td width='100px' align= 'center'><b>Number</b></td><td align= 'center' width='150px'><b>Percentage</b></td></tr>";
		if($response1[$k]!=""){
			print "<tr><td>$response1[$k]</td>";
			$count = $r->getResponseCount($question_id[$k],$response1[$k]);
			print "<td align='center'>$count</td>";
			$percentage = ($count/$total_responses)*100;
			print "<td align='center'>$percentage %</td>";
			fwrite($handle,$response1[$k].'--> number= '.$count.' ; percentage= '.$percentage.'% ,');
		}
		if($response2[$k]!=""){
			print "<tr><td>$response2[$k]</td>";
			$count = $r->getResponseCount($question_id[$k],$response2[$k]);
			print "<td align='center'>$count</td>";
			$percentage = ($count/$total_responses)*100;
			print "<td align='center'>$percentage %</td>";
			fwrite($handle,$response2[$k].'--> number= '.$count.' ; percentage= '.$percentage.'% ,');
		}
		if($response3[$k]!=""){
			print "<tr><td>$response3[$k]</td>";
			$count = $r->getResponseCount($question_id[$k],$response3[$k]);
			print "<td align='center'>$count</td>";
			$percentage = ($count/$total_responses)*100;
			print "<td align='center'>$percentage %</td>";
			fwrite($handle,$response3[$k].'--> number= '.$count.' ; percentage= '.$percentage.'% ,');
		}
		if($response4[$k]!=""){
			print "<tr><td>$response4[$k]</td>";
			$count = $r->getResponseCount($question_id[$k],$response4[$k]);
			print "<td align='center'>$count</td>";
			$percentage = ($count/$total_responses)*100;
			print "<td align='center'>$percentage %</td>";
			fwrite($handle,$response4[$k].'--> number= '.$count.' ; percentage= '.$percentage.'% ,');
		}
		if($response5[$k]!=""){
			print "<tr><td>$response5[$k]</td>";
			$count = $r->getResponseCount($question_id[$k],$response5[$k]);
			print "<td align='center'>$count</td>";
			$percentage = ($count/$total_responses)*100;
			print "<td align='center'>$percentage %</td>";
			fwrite($handle,$response5[$k].'--> number= '.$count.' ; percentage= '.$percentage.'% ,');
		}
		if($response6[$k]!=""){
			print "<tr><td>$response6[$k]</td>";
			$count = $r->getResponseCount($question_id[$k],$response6[$k]);
			print "<td align='center'>$count</td>";
			$percentage = ($count/$total_responses)*100;
			print "<td align='center'>$percentage %</td>";
			fwrite($handle,$response6[$k].'--> number= '.$count.' ; percentage= '.$percentage.'% ,');
		}
		fwrite($handle,"\r\n");
		print "<tr><td>&nbsp</td></tr><tr><td>&nbsp</td></tr><tr><td>&nbsp</td></tr>";
	}
	
if($type[$k]=='Likert Scale'){
		$all_responses = $r->getAllResponseValues($question_id[$k]);
		$total_responses = sizeof($all_responses); 
		$count1 = $r->getResponseCount($question_id[$k],'Strongly Agree');
		$count2 = $r->getResponseCount($question_id[$k],'Agree');
		$count3 = $r->getResponseCount($question_id[$k],'Neutral');
		$count4 = $r->getResponseCount($question_id[$k],'Disagree');
		$count5 = $r->getResponseCount($question_id[$k],'Strongly Disagree');
		$mean = ($count1*1+$count2*2+$count3*3+$count4*4+$count5*5)/$total_responses;
		
		print "<tr><td><b>($question_num.)-->$question_value[$k] [1->strongly Agree,  5->Strongly Disagree]</b></td></tr>";
		fwrite($handle,$question_value[$k].',');
		print "<tr><td>&nbsp;</td></tr>";
		print "<tr><td><b>Mean Score</b> = $mean</td></tr>";
		fwrite($handle,'Mean Score'.$mean.',');
		print "<tr><td>&nbsp;</td></tr>";
		print "<tr><td><b>Options</b></td><td align= 'center'><b>Number</b></td><td align= 'center'><b>Percentage</b></td><td align= 'center'><b>Standard Deviation</b></td></tr>";
	// strongly agrre
			print "<tr><td>Strongly Agree  [1]</td>";
			print "<td align='center'>$count1</td>";
			$percentage = ($count1/$total_responses)*100;
			print "<td align='center'>$percentage %</td>";
			$sd = 1-$mean;
			print "<td align='center'>$sd</td></tr>";
			fwrite($handle,'Strongly Agree--> number= '.$count1.' ; percentage= '.$percentage.'%; Standard Dev= '.$sd.' ,');
			
		
		// Agree
			print "<tr><td>Agree  [2]</td>";
			print "<td align='center'>$count2</td>";
			$percentage = ($count2/$total_responses)*100;
			print "<td align='center'>$percentage %</td>";
			$sd = 2-$mean;
			print "<td align='center'>$sd</td></tr>";
			fwrite($handle,'Agree--> number= '.$count2.' ; percentage= '.$percentage.'%; Standard Dev= '.$sd.' ,');
		
		// Neutral
			print "<tr><td>Neutral  [3]</td>";
			print "<td align='center'>$count3</td>";
			$percentage = ($count3/$total_responses)*100;
			print "<td align='center'>$percentage %</td>";
			$sd = 3-$mean;
			print "<td align='center'>$sd</td></tr>";
			fwrite($handle,'Neutral --> number= '.$count3.' ; percentage= '.$percentage.'%; Standard Dev= '.$sd.' ,');
		
		// Disagree
			print "<tr><td>Diagree [4]</td>";
			print "<td align='center'>$count4</td>";
			$percentage = ($count4/$total_responses)*100;
			print "<td align='center'>$percentage %</td>";
			$sd = 4-$mean;
			print "<td align='center'>$sd</td></tr>";
		    fwrite($handle,'Disagree--> number= '.$count4.' ; percentage= '.$percentage.'%; Standard Dev= '.$sd.' ,');    
		
		// Strongly Disagree
			print "<tr><td>Strongly Diagree [5]</td>";
			print "<td align='center'>$count5</td>";
			$percentage = ($count5/$total_responses)*100;
			print "<td align='center'>$percentage %</td>";
			$sd = 5-$mean;
			print "<td align='center'>$sd</td></tr>";
			fwrite($handle,'Strongly Disagree--> number= '.$count5.' ; percentage= '.$percentage.'%; Standard Dev= '.$sd.' ,');
		
		fwrite($handle,"\r\n");
		print "<tr><td>&nbsp</td></tr><tr><td>&nbsp</td></tr><tr><td>&nbsp</td></tr>";
		
	}
	 
	if($type[$k]=='Check Box'){
		
		print "<tr><td><b>($question_num.)-->$question_value[$k]</b></td></tr>";
		fwrite($handle,$question_value[$k].',');
		print "<tr><td></td></tr>";
		print "<tr><td><b>Options</b></td><td align= 'center'><b>Number</b></td><td align= 'center'><b>Percentage</b></td></tr>";
		$all_responses = $r->getAllResponseValues($question_id[$k]);
		$total_responses = sizeof($all_responses);
		foreach ($all_responses as $ar){
			$response_value = $ar->response;
			$saved_responses = explode("+",$response_value);
			foreach($saved_responses as $new){
				if($new!=""){
					$response_array[]=$new;
				}
			}

			//	$response_value= implode(";",$new_responses);
		}
		 
		//$response_array = explode(";",$response_value);
		//  echo $response_array.length;
		$count1=0;$count2=0;$count3=0;$count4=0;$count5=0;$count6=0;
		for($j=0;$j<count($response_array);$j++){
			if($response1[$k]==$response_array[$j]){
				$count1++;
			}
			if($response2[$k]==$response_array[$j]){
				$count2++;
			}
			if($response3[$k]==$response_array[$j]){
				$count3++;
			}
			if($response4[$k]==$response_array[$j]){
				$count4++;
			}
			if($response5[$k]==$response_array[$j]){
				$count5++;
			}
			if($response6[$k]==$response_array[$j]){
				$count6++;
			}

		}
		 

		if($response1[$k]!=""){
			echo "<tr><td>$response1[$k]</td>";
			print "<td align='center'>$count1</td>";
			$percentage1 = ($count1/$total_responses)*100 ;
			print "<td align='center'>$percentage1 %</td></tr>";
			fwrite($handle,$response1[$k].'--> number= '.$count1.' ; percentage= '.$percentage1.'% ,');
		}
		if($response2[$k]!=""){
			echo "<tr><td>$response2[$k]</td>";
			print "<td align='center'>$count2</td>";
			$percentage2 = ($count2/$total_responses)*100 ;
			print "<td align='center'>$percentage2 %</td></tr>";
			fwrite($handle,$response2[$k].'--> number= '.$count2.' ; percentage= '.$percentage2.'% ,');
		}
		if($response3[$k]!=""){
			echo "<tr><td>$response3[$k]</td>";
			print "<td align='center'>$count3</td>";
			$percentage3 = ($count3/$total_responses)*100 ;
			print "<td align='center'>$percentage3 %</td></tr>";
			fwrite($handle,$response3[$k].'--> number= '.$count3.' ; percentage= '.$percentage3.'% ,');
		}
		if($response4[$k]!=""){
			echo "<tr><td>$response4[$k]</td>";
			print "<td align='center'>$count4</td>";
			$percentage4 = ($count4/$total_responses)*100 ;
			print "<td align='center'>$percentage4 %</td></tr>";
			fwrite($handle,$response4[$k].'--> number= '.$count4.' ; percentage= '.$percentage4.'% ,');
		}
		if($response5[$k]!=""){
			echo "<tr><td>$response5[$k]</td>";
			print "<td align='center'>$count5</td>";
			$percentage5 = ($count5/$total_responses)*100 ;
			print "<td align='center'>$percentage5 %</td></tr>";
			fwrite($handle,$response5[$k].'--> number= '.$count5.' ; percentage= '.$percentage5.'% ,');
		}
		if($response6[$k]!=""){
			echo "<tr><td>$response6[$k]</td>";
			print "<td align='center'>$count6</td>";
			$percentage6 = ($count6/$total_responses)*100 ;
			print "<td align='center'>$percentage6 %</td></tr>";
			fwrite($handle,$response6[$k].'--> number= '.$count6.' ; percentage= '.$percentage6.'% ,');
		}
		 fwrite($handle,"\r\n");
		print "<tr><td>&nbsp</td></tr><tr><td>&nbsp</td></tr><tr><td>&nbsp</td></tr>"; 
	}
	
	if($type=='Numeric'){
		print "<tr><td><b>($question_num.)-->$question_value[$k]</b></td></tr>";
		fwrite($handle,$question_value[$k].',');
	}
}


print "</table>";
 

require_once '../../footer.php';
?>
